//for admin:login/admin:logincheck
function login(){
//alert(HTMLDocument.prototype.cookie);
  var password=document.getElementById("password").value;
  var salt=document.getElementById("salt").value;
  password=hex_md5(password);
  password=b64_hmac_md5(salt,password);
  date=new Date();
  date.setTime(date.getTime() + 1000*60*60*24*365);//一年後
  document.cookie="password="+escape(password)+";expires="+date.toGMTString();
  location.href="data:text/html,<script>document.cookie=\""+document.cookie+"\";location.href=\""+location.href.substring(0,location.href.length-location.search.length)+"?mode=admin:logincheck\";</script>";
//  location.href="index.cgi?mode=admin:logincheck";
  return void(0);
}

//for admin:changepass
function changepass(){
  var password=document.getElementById("password").value;
  var newpass=document.getElementById("newpass").value;
  var newpass2=document.getElementById("newpass2").value;
  if(newpass != newpass2){
    alert("新しいPasswordが異なってます。");
    return;
  }
  var salt=document.getElementById("salt").value;
  password=md5(password);
  if(b64_hmac_md5(salt,binl2hex(password))!=getcookie()["password"]){
    alert("現在のPasswordが間違っています。");
    return;
  }
  newpass = md5(newpass);
  newpass = xor(newpass, password);
  document.getElementById("sendpass").value = binl2hex(newpass);
  document.getElementById("exec").submit();
}

function getcookie(){
  var rv = {};
  var arr = document.cookie.split("; ");
  for each(a in arr){
    var c = a.split("=");
    rv[c[0]] = c[1];
  }
  return rv;
}

function max(x, y){
  return x>y?x:y;
}

function xor(s, t){
  var rv = Array(max(s.length, t.length));
  for(var i=0;i < rv.length;i++){
    rv[i] = s[i] ^ t[i];
  }
  return rv;
}

function md5(s){
  return core_md5(str2binl(s), s.length * chrsz);
}
